package com.conjane.security.services;

import com.conjane.security.entities.User;
import com.conjane.security.model.CustomUserDetails;
import com.conjane.security.repositories.UserRepository;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class JpaUserDetailsService implements UserDetailsService {

    @Resource
    private UserRepository userRepository;

    @Override
    public CustomUserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        User user = userRepository.findUserByUsername(username).orElseThrow( () -> new UsernameNotFoundException("Problem during authentication!"));
        return new CustomUserDetails(user);
    }
}
